package com.hym.travel.controller;

import com.hym.travel.entity.Province;
import com.hym.travel.service.ProvinceService;
import com.hym.travel.service.serviceImpl.ProvinceServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/province")
@CrossOrigin
@Slf4j
public class ProvinceController {

    @Autowired
    private ProvinceServiceImpl provinceService;

    @RequestMapping("/save")
    public Map<String,Object> save(@RequestBody Province province){
        Map<String,Object> map= new HashMap<>();
        try {
            provinceService.save(province);
            map.put("status",true);
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    @RequestMapping("/findByPage")
    public Map<String,Object> findByPage(Integer page,Integer rows){
        Map<String,Object> map= new HashMap<>();
        page = page == null ? 1 : page;
        log.info("page: " + page);
        rows = rows == null ? 4 : rows;
        page = (page-1) * rows;
        try {
            List<Province> provinceList = provinceService.findByPage(page, rows);
            //计算总页数
            Integer totals = provinceService.findTotals();
            Integer totalsPage = totals % rows == 0 ? totals / rows : totals / rows +1;
            map.put("totals",totals);
            map.put("totalsPage",totalsPage);
            map.put("data",provinceList);
            map.put("page", page);
            map.put("status",true);
            map.put("msg","查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","查询失败");
        }
        return map;
    }
    @RequestMapping("/delete")
    public Map<String,Object> delete( String id){
        Map<String,Object> map= new HashMap<>();
        try {
            provinceService.delete(id);
            map.put("status",true);
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @RequestMapping("/findOne")
    public Map<String,Object> findOne( String id){
        Map<String,Object> map= new HashMap<>();
        Province province = new Province();
        try {
            province = provinceService.findOne(id);
            System.out.println("province:"+province);
            map.put("province",province);
            map.put("status",true);
            map.put("msg","查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","查询失败");
        }
        return map;
    }
    @RequestMapping("/update")
    public Map<String,Object> update(@RequestBody Province province){
        Map<String,Object> map= new HashMap<>();
        try {
            provinceService.update(province);
            map.put("status",true);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","修改失败");
        }
        return map;
    }

}
